package com.largeOrange.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.largeOrange.common.R;
import com.largeOrange.pojo.Employee;
import com.largeOrange.service.EmployeeService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;



/**
 * @author 大橙子
 * @version 1.0
 * @since 1.0
 */
@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 用户登录
     *
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //1.将传过来的密码加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2.根据用户名查询用户信息
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);
        //3.判断用户是否存在
        if (emp == null) {
            return R.error("用户不存在！");
        }
        //4.判断密码是否正确
        if (!password.equals(emp.getPassword())) {
            return R.error("密码错误！");
        }
        //5.判断是否被禁用
        if (emp.getStatus() != 1) {
            return R.error("账号被禁用！");
        }
        //6.登录成功
        Long empId = emp.getId();
        request.getSession().setAttribute("employee", empId);
        return R.success(emp);
    }

    /**
     * 用户退出
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 添加用户
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> insertEmployee(@RequestBody Employee employee) {
        log.info("用户信息" + employee.toString());
        //设置默认密码加密
        String defaultPassWord = "123456";
        String md5DigestAsHex = DigestUtils.md5DigestAsHex(defaultPassWord.getBytes());
        employee.setPassword(md5DigestAsHex);
        /*
        设置新增员工时间以及更新员工时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        设置操作人员
        Long setId = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(setId);
        */
        //操作数据库
        employeeService.save(employee);
        return R.success("添加成功！");
    }

    /**
     * 分页查询
     *
     * @return
     */
    @GetMapping("/page")
    public R<Page> pageR(int page, int pageSize, String name) {
        log.info("page={},pageSize={},name={}", page, pageSize, name);
        //创建分页构造器
        Page page1 = new Page(page, pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        wrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        //添加分页查询条件
        wrapper.orderByDesc(Employee::getUpdateTime);
        //开始查询
        employeeService.page(page1, wrapper);
        return R.success(page1);
    }

    /**
     * 员工信息修改
     *
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> updateData(@RequestBody Employee employee) {
        log.info(employee.toString());
        /*添加修改日期
        employee.setUpdateTime(LocalDateTime.now());
        //添加修改人
        Long adminId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(adminId);
         */
        employeeService.updateById(employee);
        return R.success("账号修改成功！");
    }

    /**
     * 编辑员工数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> appearData(@PathVariable Long id) {
        log.info(id.toString());
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            return R.success(employee);
        }
        return R.error("账户不存在！");
    }
}
